﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSphinx.Util.Configuration;

namespace SharpSphinx.Frontend
{
    public  class DataBlocker : BaseDataProcessor 
    {
        public const String PROP_BLOCKER_SIZE_MS = "BlockSizeMS";


        private double blockSizeMs;
        private int blockSizeSamples = int.MaxValue;

        private int curFirstSamplePos;
        private LinkedList<DoubleData> inbuffer = new LinkedList<DoubleData>();

        private int curInBufferSize;
        private int sampleRate = -1;


         


        public DataBlocker()
        {

        }

        public DataBlocker(double blockSizeMS)
        {

            this.blockSizeMs = blockSizeMS;
        }



        public override void newProperties(PropertySheet ps)
        {


            blockSizeMs = ps.getDouble(PROP_BLOCKER_SIZE_MS); 


        }

        public double blockSizeMS
        {
            get
            {
                return blockSizeMs;
            }
        }


        public override IData getData()
        {

            while (curInBufferSize < blockSizeSamples || curInBufferSize == 0)
            {
                IData data = getPredecessor().getData();

                if (data is DataStartSignal)
                {
                    sampleRate = ((DataStartSignal)data).getSampleRate();
                    blockSizeSamples = (int)Math.Round(sampleRate * blockSizeMs / 1000);

                    curInBufferSize = 0;
                    curFirstSamplePos = 0;

                    inbuffer.Clear(); 

                }

                if (!(data is DoubleData))
                {
                    return data;
                }

                DoubleData dd = (DoubleData)data;

                inbuffer.AddLast(dd); 

                curInBufferSize += dd.Values.Length; 

            }

            double[] newSampleBlock = new double[blockSizeSamples];

            int copiedSamples = 0;

           long firstSample = inbuffer.ElementAt(0).FirstSampleNumber +curFirstSamplePos;
           long collectTime = inbuffer.ElementAt(0).CollectTime;

           while (inbuffer.Count != 0)
           {
               DoubleData dd; 
               inbuffer.RemoveLast();
              // double[] values = dd.Values;



           }



        }


    }
}
